<?php
/**
 * This file contains the class AlertsView which provides
 * all of the text requirements for the alerts page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
/**
 * This class provides the text for the alerts page
 *
 * @copyright  2009 A Memory Tree
 * @license    A Memory Tree License
 */
class AlertsView
{
    /**
     * A place to store pre-prepared messages
     *
     * @var array[string]string
     */
    private $_text;
    /**
      * The html class that provides helpers to display the text
     *
     * @var Html
     */
    private $_html;
    /**
     * Builds the list of text messages
     * 
     * @return void
     * @param Html &$html
     */
    public function __construct(&$html)
    {
        $this->_html = $html;
        $this->_text['sample'] = 'Sample text';
        $this->_text['nameNotSaved'] = 'Sorry, your alert needs to include a last name. Please try again.';
        $this->_text['mustAgree'] = 'You must agree to the conditions.';
        $this->_text['noAlerts'] = 'You have not created any new alerts.';
        $this->_text['cannotSave'] = 'The name could not be saved. Please try again.';
        $this->_text['cannotDelete'] = 'Unable to delete';
    }
    /**
     * Return the text that's associated with the given name
     * 
     * @return string
     * @param string $name
     */
    public function text($name)
    {
        return $this->_text[$name];
    }
    /**
     * The intro for the page
     *
     * @return string
     */
    function intro()
    {
        return <<< EOT
        <h2>My Email Alerts</h2>
        <p>Complete the form below to list the names you're searching for.</p>
        <p>The search will last for 12 months and you will be emailed every time we have a possible match.</p>
        <p>The first search is $9.90, then $5.00 for each name after that.</p>
        <p>There is no limit to how many names you want to add.</p>

EOT;
    }
    /**
     * Show the package information
     *
     * @return string
     * @param string $expires
     * @param int $remaining
     * @param string $alerts
     */
    function packageInfo($expires, $remaining, $alerts)
    {
        return <<< EOT
                    <tr>
                        <td colspan="5">Your $expires corporate package has <strong>$remaining</strong> $alerts remaining</td>
                    </tr>

EOT;
    }
    /**
     * Show the package information
     *
     * @return string
     * @param string $packageAmount
     * @param int $alertId
     * @param bool $isEnabled
     */
    function packageRow($packageAmount, $alertId, $isEnabled)
    {
        $disabled = '';
        if (!$isEnabled) {
            $disabled = ' disabled="disabled"';
        }
        return <<< EOT
                    <tr align="right">
                        <td colspan="4">Corporate package: <span class="total">$packageAmount</span></td>
                        <td><strong><input type="submit" name="package$alertId" value="1000 Alerts" class="button"$disabled></strong></td>
                    </tr>

EOT;
    }
    /**
     * Show an error message
     *
     * @return string
     * @param string $message
     */
    function error($message)
    {
        return <<< EOT
                <tr>
                    <td colspan="5" class="error"><strong>$message</strong></td>
                </tr>

EOT;
    }
    /**
     * Table for the alerts
     *
     * @return string
     * @param string $rows
     * @param int $alertId
     * @param bool $agree
     * @param string $total
     * @param string $packageInfo
     * @param string $packageRow
     * @param string $error
     */
    function alertTable($rows, $alertId, $agree, $total, $packageInfo, $packageRow, $error)
    {
        $conditionsChecked = '';
        if ($agree === TRUE) {
            $conditionsChecked = ' checked="checked"';
        }
        return <<< EOT
            <table>
                <thead>
$error
                    <tr>
                        <th>First Name</th><th>Last Name</th><th>Expires</th><th class="numeric">Price</th><th class="action">Action</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td class="numeric">&nbsp;</td>
                        <td class="action"><strong><input type="submit" name="new$alertId" value="Add New"></strong></td>
                    </tr>
                    <tr>
                        <td colspan="5"><span class="required">*</span> <input type="checkbox" name="agree" value="Yes"$conditionsChecked> <strong>Conditions:</strong> I understand that once an alert has been paid for, that I can only edit the first name of that alert. <span class="required">Please check the spelling of names.</span></td>
                    </tr>
                    <tr align="right">
                        <td colspan="4">Total order: <span class="total">$total</span></td>
                        <td><strong><input type="submit" name="order$alertId" value="Order Alerts" class="button"></strong></td>
                    </tr>
$packageInfo
$packageRow
                </tfoot>
                <tbody>
$rows
                </tbody>
            </table>

EOT;
    }
    /**
     * Show a button
     *
     * @return string
     * @param string $name
     * @param string $title
     */
    function alertButton($name, $title)
    {
        return '<input type="submit" name="'.$name.'" value="'.$title.'">';
    }
    /**
     * Show an Edit button
     *
     * @return string
     * @param int $alertId
     */
    function editAlertButton($alertId)
    {
        return $this->alertButton('edit'.$alertId, 'Edit');
    }
    /**
     * Show a Save button
     *
     * @return string
     * @param int $alertId
     */
    function saveAlertButton($alertId)
    {
        return $this->alertButton('save'.$alertId, 'Save');
    }
    /**
     * Show a Remove button for an unpaid item
     *
     * @return string
     * @param int $alertId
     */
    function removeAlertButton($alertId)
    {
        return $this->alertButton('remove'.$alertId, 'Remove');
    }
    /**
     * Show a Delete button for a paid item
     *
     * @return string
     * @param int $alertId
     */
    function deleteAlertButton($alertId)
    {
        return $this->alertButton('confirmDelete'.$alertId, 'Delete');
    }
    /**
     * Show a confirm Delete button
     *
     * @return string
     * @param int $alertId
     */
    function confirmDelete($alertId)
    {
        return <<< EOT
            Delete?
            <input type="submit" name="delete$alertId" value="Yes">
            <input type="submit" name="keep$alertId" value="No">
EOT;
    }
    /**
     * Provide a row for an alert
     *
     * @return string
     * @param int $alertId
     * @param array[string]string $alert
     * @param string $expires
     * @param bool $paid
     * @param string $amount
     * @param string $buttons
     * @param bool $editable
     */
    function alertRow($alertId, $alert, $expires, $paid, $amount, $buttons, $editable)
    {
        $firstName = $alert['firstName'];
        $lastName = $alert['lastName'];
        if ($editable) {
            $firstName = '<input type="text" name="firstname" value="'.$firstName.'" size="10">';
        }
        if ($editable && $paid === FALSE) {
            $lastName = '<span class="required">*</span> <input type="text" name="lastname" value="'.$lastName.'" size="10">';
        }
        return <<< EOT
            <tr>
                <td>$firstName</td>
                <td>$lastName</td>
                <td class="numeric">$expires</td>
                <td class="numeric">$amount</td>
                <td class="action">$buttons</td>
            </tr>

EOT;
    }
    /**
     * The add order row
     *
     * @return string
     * @param string $nextYear
     * @param string $amount
     * @param string $buttons
     */
    function addOrder($nextYear, $amount, $buttons)
    {
        return <<< EOT
                <tr>
                    <td><input type="text" name="firstname" size="10"></td>
                    <td><input type="text" name="lastname" size="10"></td>
                    <td>$nextYear</td>
                    <td class="numeric">$amount</td>
                    <td class="action">$buttons</td>
                </tr>

EOT;
    }
    /**
     * The contents for the page
     *
     * @return string
     * @param string $alerts
     */
    function content($alerts)
    {
        $html = $this->_html;
        $thisPage = $_SERVER['PHP_SELF'];
        $content = $this->intro();
        $content .= <<< EOT
        <h2>Alerts</h2>
        <form id="orderAlerts" method="POST" action="$thisPage">
$alerts
        </form>
        <h2>Our listings are obtained from published death notices</h2>
        <p>The <strong>last name</strong> you enter also searches for <strong>nee names</strong> and <strong>former names</strong>, if they have been published.</p>
        <p>The <strong>first name</strong> you enter also searches for <strong>nicknames</strong> and <strong>also known as</strong> names, if they have been published.</p>
        <p><strong>Tip:</strong> You can change the first name in your search at anytime. For optimum results simply enter the first letter of the first name.</p>

EOT;
        return $content;
    }
    /**
     * Render the page
     *
     * @return string
     * @param string $content
     */
    function render($content)
    {
        return <<< EOT
<div id="centerbar">
$content
</div>

EOT;
    }
}
?>
